/*
 * TransactionActions.java
 */

package nxtgeninventorysystem.Controller;

import java.util.ArrayList;
import nxtgeninventorysystem.Data.Transaction;
import nxtgeninventorysystem.Data.TransactionSearchOptions;
import nxtgeninventorysystem.Model.Database;
import nxtgeninventorysystem.View.Gui;

/**
 * The TransactionActions class is a Controller class inside the
 * Model-View-Controller design pattern. This class typically deals with
 * the Transaction entity in the database, allowing flow of data between the
 * View (Gui) and the Model (Database).
 * @author Seraph Softworks
 */
public final class TransactionActions extends Actions {

    public TransactionActions() {

    }

    public void gotoBrowseTransaction() {
        Thread t = new Thread() {
            public void run() {
                while(busy) {

                }
                busy = true;

                Gui.goToPage(Gui.Page.BrowseTransaction);

                busy = false;
                Actions.transaction.doSearchTransactions();
            }
        };
        t.start();
    }

    public void doSearchTransactions() {
        Thread t = new Thread() {
            public void run() {
                Gui.setEnabled(false);
                while(busy) {

                }
                busy = true;

                Gui.setBusyEffect(true);
                Gui.setStatusBarMessage("Retreiving Search Results...");

                Gui.setBrowseTransactionPage(new ArrayList <Transaction> ());

                Transaction values = Gui.getTransactionSearchValues(Gui.getCurrentPage());
                TransactionSearchOptions options = Gui.getTransactionSearchOptions(Gui.getCurrentPage());
                ArrayList <Transaction> translist = null;
                TransactionSearchOptions order = Gui.getTransactionOrderAtPage(Gui.getCurrentPage());

                switch(Gui.getCurrentPage()) {
                    case BrowseTransaction:
                        translist = Database.transaction.searchTransaction(values, options, order, true);
                        Gui.setBrowseTransactionPage(translist);
                        break;
                }

                Gui.setBusyEffect(false);
                Gui.setStatusBarMessage("Ready...");

                Gui.setEnabled(true);

                busy = false;
            }
        };
        t.start();
    }

    public void gotoReadTransaction() {
        Thread t = new Thread() {
            public void run() {
                Gui.setEnabled(false);
                while(busy) {

                }
                busy = true;

                Gui.setBusyEffect(true);
                Gui.setStatusBarMessage("Retreiving Transaction Information...");

                Transaction transaction = null;

                switch(Gui.getCurrentPage()) {
                    case ReadTransaction:
                        transaction = Gui.getReadTransactionTarget();
                        break;
                    default:
                        ArrayList <Transaction> transactions = Gui.getTransactionsSelectedAtPage(Gui.getCurrentPage());
                        if(transactions == null)
                            return;
                        transaction = transactions.get(0);
                        break;
                }

                if(transaction == null)
                    return;

                Gui.setReadTransaction(Database.transaction.readTransaction(transaction.getId()), Database.transaction.getTransactionItems(transaction.getId(),Gui.getItemOrderAtPage(Gui.Page.ReadTransaction),true));

                Gui.setBusyEffect(false);
                Gui.setStatusBarMessage("Ready...");

                Gui.setEnabled(true);

                Gui.goToPage(Gui.Page.ReadTransaction);

                busy = false;
            }
        };
        t.start();
    }

}
